Hello,
I will try to explain my question. I am pretty new in C. And i want to program a text modul.
Using dynamic struct and dynamic arrays. it is not a homework its nor more like preperation for a test.
text stuctur: length without \0
dynamic array from type char and \0 should be save
Text prototype:
create(): parameter size, create a new text with a dynamic array, size define size.
it should have more place bcs of ´\0.
init(): parameter c string, with new size, text needs to be create()
data needs to copied by strlen() and strcpy(), it has to be returned
kill(): ofc delete dynamic array, set size on 0 and the pointer on Null
add(): two text struct on value: both text gets to one text.
Firstly the sum of both lengths is the new size. create() makes a new text
then copy the data. Finally return.
print(): text struct on value, data needs to get print.
The Code:
Code:
struct text {
int length;
char *zeichen;};
struct text create(int size);
struct text init(char string[]);
void kill(struct text *rS);
struct text add(struct text a, struct text b);
void print();
struct
text create(int size) {
struct text s = { NULL, size }; s.zeichen = malloc((size + 1) * sizeof(char));
if (s.zeichen != NULL) {
s.length = size;}
return s;}
void kill(struct text *rS) {
if (rS->zeichen != NULL)
free(rS->zeichen); rS->zeichen = NULL; rS->length = 0;}
struct text init(char zeichen[]) { // copy text and a new length
struct text newtext = create(strlen(zeichen)); strcpy(newtext, zeichen); //copy the new text
return newtext;}
struct text add(struct text a, struct text b) {
struct text longtext = create(a.length + b.length); //size of both text
strcat(longtext, a); // text a need to get add to b
return b;}
void print(struct text q) {
printf("%s", q.zeichen);}